const http = require('http');
const { createProxyMiddleware } = require('http-proxy-middleware');

const server = http.createServer((req, res) => {

    const url = req.url;

    if (/\/strategy/.test(url)) {
        console.log('ss');
        const proxy = createProxyMiddleware('/strategy', {
            target: 'https://pcw-api.iqiyi.com',
            changeOrigin: true
        })

        proxy(req, res);
    } else if (/\/list/.test(url)) {
        console.log('aa');
        const proxy2 = createProxyMiddleware('/list', {
            target: 'https://apisgame.iqiyi.com',
            changeOrigin: true,
            pathRewrite: {
                '^/list': ''
            }, 
            router: {
                'localhost:8080': 'https://pcw-api.iqiyi.com' // 当localhost:8080端口请求时，将https://apisgame.iqiyi.com 替换成 https://pcw-api.iqiyi.com
            }
        })

        proxy2(req, res);
    } else {
        console.log('error');
    }
    
}).listen(8080)